package com.insist.util.aliyun;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import org.apache.commons.io.IOUtils;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;


public abstract class OSSObjectUtilOrignalBase extends OSSObjectUtilBase {

    protected OSSObjectUtilOrignalBase(String accessKeyID, String accessKeySecret, String bucketName) {
        super(accessKeyID, accessKeySecret, bucketName);
    }


    public PutObjectResult uploadByStreamNew(String key, byte[] fileBytes,String contentType) throws OSSException, ClientException, IOException {
        return uploadByStreamInnerNew(key, fileBytes, contentType);
    }


    private PutObjectResult uploadByStreamInnerNew(String key,byte[] fileBytes,String contentType) throws OSSException, ClientException, IOException{
        ObjectMetadata objectMeta = new ObjectMetadata();
        // 可以在metadata中标记文件类型
        objectMeta.setContentType(contentType);
        objectMeta.setContentLength(fileBytes.length);
        InputStream input = null;
        try {
            input = new BufferedInputStream(new ByteArrayInputStream(fileBytes));
            PutObjectResult por = internalClient.putObject(bucketName, key, input, objectMeta);
            return por;
        }finally {
            IOUtils.closeQuietly(input);
        }

    }


}
